//
//  HomePageBeforeVC.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/20.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "HomePageBeforeVC.h"
#import "HomePageBeforeListVC.h"
#import "HomePageDataModel.h"
@interface HomePageBeforeVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) YHYBaseTableView *beforeTableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@end

@implementation HomePageBeforeVC

- (void)dealloc {
    
    _beforeTableView.delegate = nil;
    
    _beforeTableView.dataSource = nil;
    
    [_beforeTableView release];
    [_dataSource release];
    [_month release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatView];

}

#pragma mark - 创建视图

#pragma mark - month setter方法

- (void)setMonth:(NSMutableString *)month {
    
    if (_month != month) {
        [_month release];
        _month = [month retain];
    }
    
    /**
     *  通过字符串的删除方法 截取年月份 (201604)
     */
    [_month deleteCharactersInRange:NSMakeRange(10, 9)];
    [_month deleteCharactersInRange:NSMakeRange(4, 1)];
    [_month deleteCharactersInRange:NSMakeRange(6, 3)];
    
    /**
     *  通过循环 遍历给定 年月份 (2016-04)
     */
    self.dataSource = [NSMutableArray array];
    
    for (int i = _month.intValue; i >= 201210; i--) {
        
        if (i % 100 == 0) {
            i = i - 100 + 12;
        }
        
        [self.dataSource addObject:[NSString stringWithFormat:@"%d-%02d", i / 100, i % 100]];
        
    }
    [self.beforeTableView reloadData];
}


- (void)creatView {
    
    //自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"往期列表";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor lightGrayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.leftBarButtonItem = item;
    
    
    /**
     *  创建tableView
     */
    self.beforeTableView = [[YHYBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) style:UITableViewStylePlain];
    [_beforeTableView registerClass:[YHYBaseTableViewCell class] forCellReuseIdentifier:@"before"];
    _beforeTableView.delegate = self;
    _beforeTableView.dataSource = self;
    _beforeTableView.backgroundColor = [UIColor colorWithRed:245/255.0 green:249/255.0 blue:279/255.0 alpha:1];
    _beforeTableView.bounces = NO;
    [self.view addSubview:_beforeTableView];
    
    [_beforeTableView release];
    
}

- (void)itemAction:(UIBarButtonItem *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark - tableView协议方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (_dataSource.count != 0) {
        return self.dataSource.count;
    }
    
    return 0;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YHYBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"before"];
    cell.backgroundColor = [UIColor colorWithRed:245/255.0 green:249/255.0 blue:279/255.0 alpha:1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.textLabel.text = @"本月";
    }else {
        cell.textLabel.text = self.dataSource[indexPath.row];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HomePageBeforeListVC *listVC = [[HomePageBeforeListVC alloc] init];
    
    [self.navigationController pushViewController:listVC animated:YES];
    
    listVC.month = self.dataSource[indexPath.row];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
